﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinformPratice
{
    public partial class ImageCtrl : Form
    {
        public ImageCtrl()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap map = (Bitmap)pictureBox1.Image;
            for (int i = 0; i < map.Width; i++)
            {
                for (int j = 0; j < map.Height; j++)
                {
                    Color old = map.GetPixel(i, j);
                    map.SetPixel(i, j, Gray(old));
                }
            }
            pictureBox2.Image = map;
        }


        protected Color Gray(Color c)
        {
            int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));
            return Color.FromArgb(rgb, rgb, rgb);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap map = (Bitmap)pictureBox1.Image;
            Binarizate(map);
            pictureBox3.Image = map;
        }

        public void Binarizate(Bitmap map)
        {
            int tv = ComputeThresholdValue(map);
            int x = map.Width;
            int y = map.Height;
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    if (map.GetPixel(i, j).R >= tv)
                    {
                        map.SetPixel(i, j, Color.FromArgb(0xff, 0xff, 0xff));
                    }
                    else
                    {
                        map.SetPixel(i, j, Color.FromArgb(0, 0, 0));
                    }
                }
            }
        }

        private int ComputeThresholdValue(Bitmap img)
        {
            int i;
            int k;
            double csum;
            int thresholdValue = 1;
            int[] ihist = new int[0x100];
            for (i = 0; i < 0x100; i++)
            {
                ihist[i] = 0;
            }
            int gmin = 0xff;
            int gmax = 0;
            for (i = 1; i < (img.Width - 1); i++)
            {
                for (int j = 1; j < (img.Height - 1); j++)
                {
                    int cn = img.GetPixel(i, j).R;
                    ihist[cn]++;
                    if (cn > gmax)
                    {
                        gmax = cn;
                    }
                    if (cn < gmin)
                    {
                        gmin = cn;
                    }
                }
            }
            double sum = csum = 0.0;
            int n = 0;
            for (k = 0; k <= 0xff; k++)
            {
                sum += k * ihist[k];
                n += ihist[k];
            }
            if (n == 0)
            {
                return 60;
            }
            double fmax = -1.0;
            int n1 = 0;
            for (k = 0; k < 0xff; k++)
            {
                n1 += ihist[k];
                if (n1 != 0)
                {
                    int n2 = n - n1;
                    if (n2 == 0)
                    {
                        return thresholdValue;
                    }
                    csum += k * ihist[k];
                    double m1 = csum / ((double)n1);
                    double m2 = (sum - csum) / ((double)n2);
                    double sb = ((n1 * n2) * (m1 - m2)) * (m1 - m2);
                    if (sb > fmax)
                    {
                        fmax = sb;
                        thresholdValue = k;
                    }
                }
            }
            return thresholdValue;
        }

        public void SplitNew(Bitmap map, int count)
        {
            if (count <= 0)
            {
                throw new ArgumentOutOfRangeException("Count 必须大于0.");
            }

            int mapWidth = map.Width;
            int mapHeight = map.Height;
            int[] XCount = new int[mapWidth];

            for (int i = 0; i < mapWidth; i++)
            {
                for (int j = 0; j < mapHeight; j++)
                {
                    if (map.GetPixel(i, j).R == 0)
                        XCount[i]++;
                }
            }

            int beg=0, end=0;
            for (int i = 0; i < mapWidth; i++)
            {
                if (XCount[i] > 1)
                {
                    beg = i;
                    break;
                }
            }
            for (int i = mapWidth - 1; i > 0; i--)
            {
                if (XCount[i] > 1)
                {
                    end = i;
                    break;
                }
            }
            richTextBox1.AppendText("beg:"+beg+"\tend:"+end+"\n");
            for (int i = 0; i < mapWidth; i++)
            {
                richTextBox1.AppendText(XCount[i].ToString() + ",");
            }

            int len = (end - beg - 6) / 4;
            richTextBox1.AppendText("len:" + len +"\n");
            int length = 0;
            bool start = false;
            int iCount = 0;
            IList<Pair> pairList = new List<Pair>();
            for (int i = 0; i < mapWidth; i++) {

                if (XCount[i] > 0 && length < len)
                {
                    if (start == false)
                    {
                        start = true;
                        iCount++;
                        richTextBox1.AppendText("i:" + iCount + "\t");
                    }
                    length++;
                    richTextBox1.AppendText(XCount[i].ToString() + ",");
                }
                else {
                    if (start == true) {
                        start = false;
                        richTextBox1.AppendText("\n");
                    }
                    length = 0;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SplitNew((Bitmap)pictureBox3.Image, 4);
        }

    }
}